package serive.imp;

import java.sql.SQLException;
import java.util.HashMap;

import dao.imp.PetDaoImp;
import dao.imp.PetStoreDaoImp;
import dao.imp.PetownerDaoImp;
import dao.imp.StoreDaoImp;
import model.Pet;
import model.PetStore;
import model.Petowner;
import model.Store;
import serive.PetownerSerive;
import tool.Datae;

public class PetownerSeriveImp implements PetownerSerive{
	PetownerDaoImp own=new PetownerDaoImp();
	PetDaoImp pet = new PetDaoImp();
	StoreDaoImp store = new StoreDaoImp();
	PetStoreDaoImp deal = new PetStoreDaoImp();
	static HashMap<String, Petowner> ownMap = new HashMap<String, Petowner>();
	static HashMap<String, Pet> petMap = new HashMap<String, Pet>();
	static HashMap<String, Store> storeMap = new HashMap<String, Store>();
	static HashMap<String, PetStore> petstoreMap = new HashMap<String, PetStore>();
	static ShowImp show = new ShowImp();
	@Override
	public HashMap<String, Petowner> show() throws SQLException {
		// TODO Auto-generated method stub
		
		return own.add();
	}

	@Override
	public boolean buy(int buyid, int petid, int sellid) throws SQLException {
		// TODO Auto-generated method stub
		ownMap = show.showOwn();
		petMap = show.showPet();
		storeMap = show.showStore(); 
		int petprice = petMap.get(petid+"").getMoney();
        int startOwn = ownMap.get(buyid+"").getmoney();
        int startStore = storeMap.get(sellid+"").getmoney();
       
        startOwn -= petprice;
        startStore += petprice;
   
		ownMap.get(buyid+"").setmoney(startOwn);
		storeMap.get(sellid+"").setmoney(startStore);
		petMap.get(petid+"").setOwn_Id(buyid);
		
		Datae time = new Datae();
		petstoreMap.put(time.getstringdata()+"", new PetStore(time.getintdata(), 1,petid,sellid , buyid,  petprice, time.getstringdata()));
		
		deal.update(petstoreMap.get(time.getstringdata()+""));
		own.update(ownMap.get(buyid+""));
		pet.update(petMap.get(petid+""));
		store.update(storeMap.get(sellid+""));
		
		return true;
	}
	public boolean sell(int buyid, int petid, int sellid) throws SQLException {
		// TODO Auto-generated method stub
		ownMap = show.showOwn();
		petMap = show.showPet();
		storeMap = show.showStore(); 
		int petprice = petMap.get(petid+"").getMoney();
        int startOwn = ownMap.get(buyid+"").getmoney();
        int startStore = storeMap.get(sellid+"").getmoney();
        
        startOwn +=petprice;
        startStore -= petprice;
  
		ownMap.get(buyid+"").setmoney(startOwn);
		storeMap.get(sellid+"").setmoney(startStore);
		petMap.get(petid+"").setOwn_Id(buyid);
		Datae time = new Datae();
		petstoreMap.put(time.getstringdata()+"", new PetStore(time.getintdata(), 2, petid, buyid, sellid, petprice, time.getstringdata()));
		
		
		deal.update(petstoreMap.get(time.getstringdata()+""));
		own.update(ownMap.get(buyid+""));
		pet.update(petMap.get(petid+""));
		store.update(storeMap.get(sellid+""));
		
		return true;
	}

}
